package com.neusoft.neuiotms.dcom.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.UserAuthorityConditionConverter;
import com.neusoft.neuiotms.dcom.dto.DutyArrangementDTO;
import com.neusoft.neuiotms.dcom.dto.DutyArrangementGroupDTO;
import com.neusoft.neuiotms.dcom.service.DutyArrangementService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "值班安排管理")
@RestController
@RequestMapping(value = "/api/w/dutyarrangements")
public class DutyArrangementController {

    @Autowired
    private DutyArrangementService dutyArrangementService;
    @Autowired
    private UserAuthorityConditionConverter userAuthorityConditionConverter;

    @ApiOperation("查询所有值班安排")
    @GetMapping
    public ResultListDTO<DutyArrangementDTO> index(final Searchable searchable, final UserBean user) {
        this.userAuthorityConditionConverter.addUserDownOrgCodesAuthoritySearchCondition(searchable, user);
        return ResultListDTO.success(this.dutyArrangementService.index(searchable));
    }

    @ApiOperation("分页查询所有值班安排")
    @GetMapping("/p")
    public PageResultDTO<DutyArrangementDTO> search(final Pageable pageable, final Searchable searchable,
            final UserBean user) {
        this.userAuthorityConditionConverter.addUserDownOrgCodesAuthoritySearchCondition(searchable, user);
        return PageResultDTO.success(this.dutyArrangementService.search(pageable, searchable));
    }

    @ApiOperation("创建值班安排")
    @PostMapping("")
    public ResultDTO<DutyArrangementDTO> create(@RequestBody final DutyArrangementDTO dto) {
        return ResultDTO.success(this.dutyArrangementService.create(dto));
    }

    @ApiOperation(value = "修改值班安排")
    @PutMapping("/{id}")
    public ResultDTO<DutyArrangementDTO> update(@PathVariable final Long id,
            @RequestBody final DutyArrangementDTO dto) {
        return ResultDTO.success(this.dutyArrangementService.update(id, dto));
    }

    @ApiOperation(value = "修改值班安排--两组互换")
    @PutMapping("/{id}/order")
    public ResultDTO<DutyArrangementDTO> order(@PathVariable final Long id,
            @RequestBody final List<DutyArrangementGroupDTO> dtos) {
        return ResultDTO.success(this.dutyArrangementService.update(id, dtos));
    }

    @ApiOperation("值班安排详情")
    @GetMapping("/{id}")
    public ResultDTO<DutyArrangementDTO> details(@PathVariable final Long id) {
        return ResultDTO.success(this.dutyArrangementService.details(id));
    }

    @ApiOperation("删除值班安排")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.dutyArrangementService.delete(id);
        return ResultDTO.success();
    }
}
